package as.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import as.entity.AttendanceRecord;
import as.entity.Student;
import as.util.Date2String;

public class AttendanceRecordDAO extends HibernateDaoSupport {
	public boolean add(AttendanceRecord record) {
		try {
			this.getHibernateTemplate().save(record);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;

	}

	public boolean update(AttendanceRecord record) {
		try {
			this.getHibernateTemplate().update(record);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public AttendanceRecord getUnSignOut(Date date ,int registerNo) {
		String hql="from AttendanceRecord as record " +
				"where record.date = ?  and record.register = ? and record.signOut is null";
		Session session= this.getSession();
		AttendanceRecord record=(AttendanceRecord)	
		session.createQuery(hql).setDate(0, date).
		setInteger(1, registerNo).uniqueResult();
		
		session.close();
		
	 
		return record ;
	}
	public List<AttendanceRecord> list(long courseId,long studentId,Date startTime ,Date endTime){
		String hql="from AttendanceRecord as record where" +
				" record.date >= ? and  record.date <= ?  " +
				" and record.register.course = ? and record.register.student = ?";
		Session session= this.getSession();
		List<AttendanceRecord> recordList=(List<AttendanceRecord>)	
		session.createQuery(hql).setDate(0, startTime).setDate(1,endTime).
		setLong(2, courseId).setLong(3,studentId ).list();
		
		session.close();
		
		return recordList;
	}
	public List<Student> attendanceOfDate(long courseId,Date date){
		String hql="select record.register.student from  AttendanceRecord as record where record.date= ? " +
				" and record.register.course=? and record.signOut is null ";
		Session session= this.getSession();
		List<Student> list=(List<Student>)	
		session.createQuery(hql).setDate(0,date).
		setLong(1, courseId).list();
		
		session.close();
		for(Student student:list)
			student.setIn(true);
		return list;
		
	}

}
